﻿using System;

namespace Aeon.Configuration
{
    /// <summary>
    /// Describes a device configuration registration.
    /// </summary>
    public sealed class DeviceConfigurationInfo
    {
        /// <summary>
        /// The type of the device configuration.
        /// </summary>
        private Type deviceType;

        /// <summary>
        /// Initializes a new instance of the DeviceConfigurationInfo class.
        /// </summary>
        /// <param name="configType">The type of the device configuration.</param>
        internal DeviceConfigurationInfo(Type configType)
        {
            var attrs = configType.GetCustomAttributes(typeof(DeviceConfigurationAttribute), false);
            var configAttribute = (DeviceConfigurationAttribute)attrs[0];
            if(!string.IsNullOrEmpty(configAttribute.Id))
                this.Id = configAttribute.Id;
            else
                this.Id = configType.FullName;

            this.deviceType = configType;
        }

        /// <summary>
        /// Gets the unique ID of the device configuration.
        /// </summary>
        public string Id { get; private set; }
        /// <summary>
        /// Gets the type of the device configuration.
        /// </summary>
        public Type ConfigType
        {
            get { return this.deviceType; }
        }

        /// <summary>
        /// Creates a new instance of the device configuration type.
        /// </summary>
        /// <returns>New instance of the device configuration.</returns>
        public IDeviceConfiguration Create()
        {
            return (IDeviceConfiguration)Activator.CreateInstance(this.deviceType);
        }
    }
}
